package com.web.config;

import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.web.Controller.*;
import com.web.model.*;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

public class MyConfig extends JFinalConfig {
	/**
     * 供Shiro插件使用。
     */
    Routes routes;
	public void configConstant(Constants me) {
		loadPropertyFile("jfinal.properties");
		
		me.setDevMode(getPropertyToBoolean("devMode"));
		me.setMainRenderFactory(new BeetlRenderFactory());
		// 获取GroupTemplate ,可以设置共享变量等操作
		GroupTemplate groupTemplate = BeetlRenderFactory.groupTemplate ;
		//RequiresGuest，RequiresAuthentication，RequiresUser验证异常，返回HTTP401状态码
		me.setErrorView(401, "/401.jsp");
		//RequiresRoles，RequiresPermissions授权异常,返回HTTP403状态码
		me.setErrorView(403, "/403.jsp");
		me.setError404View("/404.jsp");
		me.setError500View("/500.jsp");
		
		
	}
	public void configRoute(Routes me) {
		this.routes = me;
		me.add("/", HomeController.class);
		me.add("/register", RegisterController.class);
		me.add("/public",PublicController.class);
		me.add("/login",LoginController.class);
		me.add("/adminmg",AdminmgController.class);
		me.add("/usermg",UsermgController.class);
	}
	public void configPlugin(Plugins me) {
		//数据库配置
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("username"), getProperty("password"), getProperty("driverClass"));
		WallFilter wallFilter = new WallFilter();
		wallFilter.setDbType("mysql");
		druidPlugin.addFilter(wallFilter);
		me.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.addMapping("users","id", UsersModel.class);
		arp.setShowSql(true);    //这句话就是ShowSql
		me.add(arp);
		//加载Shiro插件配置
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
		shiroPlugin.setLoginUrl("/login");
		shiroPlugin.setUnauthorizedUrl("/404.jsp");//没有权限的时候跳转页面
		me.add(shiroPlugin);
	}
	public void configInterceptor(Interceptors me) {
//		配置了一个全局拦截器
		me.add(new ShiroInterceptor());
	}
	public void configHandler(Handlers me) {}
}
